<?php
namespace app\common\controller;

use think\Controller;
use think\facade\Env;
use think\facade\Config;
use think\facade\Session;
use app\common\model\Options as OptionsModel;

class Base extends Controller
{
    public function initialize()
    {
        // 读取网站设置
        $site_root = Env::get('app.site_root', '');
        $this->assign('site_root', $site_root);
        $cdn_root = Env::get('app.cdn_root', '');
        $this->assign('cdn_root', $cdn_root);

        $static_version = Env::get('app.static_version', '');
        if(empty($static_version)){
            $static_version = time();
        }
        $this->assign('static_version', $static_version);

        // 网站SEO信息
        $this->readSiteSetting();

        $this->request = request();
    }

    /**
     * 读取Session Flush消息
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-08
     * @return   [type]             [description]
     */
    protected function readFlushMessage(){
        $flushes = [];
        $names = ['info', 'success', 'danger'];
        foreach ($names as $key => $name) {
            if(Session::has($name)){
                $flushes[$name] = Session::pull($name);
            }
        }
        $this->assign('flushes', $flushes);
    }

    public function _empty($name)
    {
        //把所有城市的操作解析到city方法
        $this->redirect('index');
    }

    /**
    * 获取表单请求参数
    * @Author   Laifuzi
    * @DateTime 2018-06-06
    * @param    [type]     $fields       [description]
    * @param    array      $default_data [description]
    * @return   [type]                   [description]
    */
    public function parsePostParam($fields, $default_data = []){
        if(is_array($default_data)){
            $data = $default_data;
        }else{
            $data = [];
        }

        $param = $this->request->post();
        foreach ($fields as $key => $field_item) {
            if(!isset($field_item['name'])){
                continue;
            }
            $name = $field_item['name'];
            if(isset($param[$name])){
                $val = $param[$name];
            }else if(isset($field_item['is_null']) && $field_item['is_null']){
                continue;
            }else if(isset($field_item['default'])){
                $val = $field_item['default'];
            }

            if(isset($field_item['type'])){
                $field_type = $field_item['type'];
            }else{
                $field_type = 'string';
            }
            switch ($field_type) {
                case 'integer':
                    $val = intval($val);
                    break;
                case 'boolean':
                    $val = boolval($val);
                    break;
                case 'arr2str':
                    if(is_array($val)){
                        $val = implode(',', $val);
                    }
                    break;
                case 'int_time':
                    if(empty($val)){
                        $val = 0;
                    }else{
                        $val = strtotime($val);
                    }
                    break;
                case 'arr2str':
                    if(is_array($val)){
                        $val = implode(",", $val);
                    }else{
                        $val = "";
                    }
                    break;
                case 'date':
                    if(empty($val)){
                        $val = $field_item['default'];
                    }
                    break;
                case 'birthday':
                    if(empty($val)){
                        $val = $field_item['default'];
                    }
                    $today_str = date('Y-m-d');
                    if(strtotime($val) > strtotime($today_str)){
                        $val = $today_str;
                    }
                    break;
                case 'date2int':
                    if(empty($val)){
                        $val = 0;
                    }else{
                        $val = strtotime($val);
                    }
                    break;
                case 'end_date2int':
                    if(empty($val)){
                        $val = 0;
                    }else{
                        $val = strtotime($val.' 23:59:59');
                    }
                    break;
                case 'area_text':
                case 'rich_text':
                    $config = Config::get('purifier.settings.user_topic_body');
                    $clean_config = \HTMLPurifier_Config::create($config);
                    $purifier = new \HTMLPurifier($clean_config);
                    $val = $purifier->purify($val);
                    break;
                default:
                    $val = trim($val);
                    break;
            }

            $data[$name] = $val;
        }
        return $data;
    }

    protected function index_default_param(){
        return [];
    }

    protected function readSiteSetting(){
        $OptionsModel = new OptionsModel();
        $options = $OptionsModel->readSetting("site_options");

        $this->seo = [];
        $alias_fields = [
            'site_name' => 'site_name',
            'site_seo_title' => 'title',
            'site_seo_keywords' => 'keywords',
            'site_seo_description' => 'description',
        ];
        if(empty($options)){
            foreach ($alias_fields as $key => $name) {
                $this->seo[$name] = '';
            }
        }else{
            foreach ($alias_fields as $key => $name) {
                if($name == 'seo_title'){
                    $val = $options[$key];
                    if(empty($val)){
                        $this->seo[$name] = $options['site_name'];
                    }else{
                        $this->seo[$name] = $val."－".$options['site_name'];
                    }
                }else{
                    $this->seo[$name] = $options[$key];
                }
                unset($options[$key]);
            }
        }
        $this->assign('site', $options);
        $this->assign('seo', $this->seo);
    }

    /**
     * 设置单个页面的SEO信息
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2018-06-26
     * @param    array             $opts [description]
     */
    protected function setPageSeo($opts = []){
        $allow_names = ['title', 'description', 'keywords'];
        foreach ($allow_names as $name) {
            if(isset($opts[$name]) && !empty($opts[$name])){
                $val = $opts[$name];
                if($name == 'title'){
                    $val = $val."－".$this->seo['site_name'];
                }
                $this->seo[$name] = $val;
            }
        }
        $this->assign('seo', $this->seo);
    }
}
